package com.csii.pos.news;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csii.adhoc.id.UidGenerator;
import com.csii.adhoc.controller.BaseController;
import com.csii.pos.news.service.NewsService;
import com.csii.result.APIResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @description: 提示消息管理
 * @author: liuzhonghua
 * @create:2021.7.14
 */
@RequestMapping("news")
@Slf4j
@RestController
public class NewsController extends BaseController<NewsManage, NewsService> {
    @Resource
    private NewsService newsService;
    @Resource
    private UidGenerator uidGenerator;
    @RequestMapping("addNews")
    /**
     * @description: 提示消息管理新增
     * @param: newsManage
     * @return:  无
     * @author liuzhonghua
     * @date:
     */
    public APIResult<?> addNews(@RequestBody NewsManage newsManage){
        //获取当前时间存入创建时间中
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
        //当前日期
        String format = df.format(new Date());
        newsManage.setCreatedDt(format);
        //将处理标识设置为未处理
        if (StringUtils.isEmpty(newsManage.getNewsFlag())) {
            newsManage.setNewsFlag("0");
        }
        newsService.save(newsManage);
        //主键
        long uid = uidGenerator.getUID();
        newsManage.setNewsId(String.valueOf(uid));
        return APIResult.successResult("添加成功！");
    }
    /**
     * @description: 提示消息管理查询
     * @param:  无
     * @return:  全部为处理的消息
     * @author liuzhonghua
     * @date:
     */
    @RequestMapping("queryNews")
    public APIResult<?> queryNews(){
        QueryWrapper<NewsManage> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("NEWS_FLAG","0");
        List<NewsManage> list = newsService.list(queryWrapper);
        return APIResult.successResult(list);
    }
    /**
     * @description: 提示消息管理处理提示
     * @param: newsManage
     * @return:  无
     * @author liuzhonghua
     * @date:
     */
    @RequestMapping("manageNews")
    public APIResult<?> manageNews(@RequestBody NewsManage newsManage){
        if (StringUtils.isEmpty(newsManage.getNewsFlag())) {
            newsManage.setNewsFlag("1");
        }else if (StringUtils.isEmpty(newsManage.getConductorDt())){
            //获取当前时间存入处理时间
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
            //当前日期
            String format = df.format(new Date());
            newsManage.setConductorDt(format);
        }
        newsService.updateById(newsManage);
        return APIResult.successResult("修改成功");
    }
}
